<?php

namespace app\data\service;

use think\admin\Service;
//require_once 'vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Ocr\V20181119\OcrClient;
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
use think\Exception;

/**
 * 腾讯服务
 * Class GoodsService
 * @package app\data\service
 */
class TencentSerice extends Service
{
    /**
     * 获取鉴权签名
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function autograph()
    {
        $appid = "1312825373";
        $bucket = "tencentyun";
        $secret_id = "AKIDDG3artdGh27wWquiGYaLp2yd5IH0TlnZ";
        $secret_key = "WmmWzB5j7E4cuwjHYYHj0zh8HAm7Byaz";
        $expired = time() + 2592000;
        $onceExpired = 0;
        $current = time();
        $rdm = rand();
        $userid = "0";
        $fileid = "";
        $srcStr = 'a='.$appid.'&b='.$bucket.'&k='.$secret_id.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&f=';
        $srcWithFile = 'a='.$appid.'&b='.$bucket.'&k='.$secret_id.'&e='.$expired.'&t='.$current.'&r='.$rdm.'&f='.$fileid;
        $srcStrOnce= 'a='.$appid.'&b='.$bucket.'&k='.$secret_id.'&e='.$onceExpired .'&t='.$current.'&r='.$rdm.'&f='.$fileid;

        $signStr = base64_encode(hash_hmac('SHA1', $srcStr, $secret_key, true).$srcStr);
        $srcWithFile = base64_encode(hash_hmac('SHA1', $srcWithFile , $secret_key, true).$srcWithFile );
        $signStrOnce = base64_encode(hash_hmac('SHA1',$srcStrOnce,$secret_key, true).$srcStrOnce);
//        echo $signStr."\n";
//        echo $srcWithFile ."\n";
//        echo $signStrOnce."\n";
        return $signStr;
    }

    /**
     * 身份证识别
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function distinguish($image)
    {
        $secret_id = "AKIDDG3artdGh27wWquiGYaLp2yd5IH0TlnZ";
        $secret_key = "WmmWzB5j7E4cuwjHYYHj0zh8HAm7Byaz";
        $cred = new Credential($secret_id, $secret_key);

        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("ocr.tencentcloudapi.com");

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new OcrClient($cred, "ap-beijing", $clientProfile);
        $req = new IDCardOCRRequest();
        $params = array(
            "ImageUrl" => $image,//身份证图片地址
        );

        $req->fromJsonString(json_encode($params));

        $resp = $client->IDCardOCR($req);

       // print_r($resp->toJsonString());die;
        return json_decode($resp->toJsonString(),true);
    }
}